home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Java for Macintosh Applications / Edit / Edit.java < prev    next >
Encoding:
Java Source  |  1997-06-27  |  7.5 KB  |  412 lines  |  [TEXT/SNIJ]

  1. /*
  2.     Edit.java
  3.  
  4.     Really simple Java text editor.
  5.  
  6.     © 1997 by Michael J. Webb (mjw@codewell.com)
  7.  
  8. */
  9.  
  10. /*
  11.     This class is an extension of the Frame class for use as the
  12.     main window of an application.
  13.  */
  14.  
  15. import java.awt.*;
  16.  
  17. import java.lang.Long;
  18.  
  19. import java.io.DataInputStream;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileOutputStream;
  23. import java.io.PrintStream;
  24.  
  25. public class Edit extends Frame
  26. {
  27.  
  28. /* Construction and Destruction Methods. */
  29.  
  30.     // Default constructor.  Builds the window layout.
  31.     public Edit()
  32.     {
  33.         super();
  34.         
  35.         //{{INIT_MENUS
  36.         menuBar = new java.awt.MenuBar();
  37.  
  38.         fileMenu = new java.awt.Menu("File");
  39.         fileMenu.add("New");
  40.         fileMenu.add("Open…");
  41.         fileMenu.addSeparator();
  42.         fileMenu.add("Close");
  43.         fileMenu.add("Save");
  44.         fileMenu.add("Save As…");
  45.         fileMenu.addSeparator();
  46.         fileMenu.add("Quit");
  47.         menuBar.add(fileMenu);
  48.  
  49.         editMenu = new java.awt.Menu("Edit");
  50.         editMenu.add("Undo");
  51.         editMenu.addSeparator();
  52.         editMenu.add("Cut");
  53.         editMenu.add("Copy");
  54.         editMenu.add("Paste");
  55.         menuBar.add(editMenu);
  56.         setMenuBar(menuBar);
  57.         //}}
  58.         
  59.         //{{INIT_CONTROLS
  60.         GridBagLayout gridBagLayout;
  61.         gridBagLayout = new GridBagLayout();
  62.         setLayout(gridBagLayout);
  63.         resize(insets().left + insets().right + 558,insets().top + insets().bottom + 441);
  64.         panel1 = new java.awt.Panel();
  65.         panel1.setLayout(null);
  66.         panel1.reshape(insets().left + 0,insets().top + 0,125,441);
  67.         panel1.setBackground(new Color(12632256));
  68.         GridBagConstraints gbc;
  69.         gbc = new GridBagConstraints();
  70.         gbc.gridx = 0;
  71.         gbc.gridy = 0;
  72.         gbc.weighty = 1;
  73.         gbc.anchor = GridBagConstraints.NORTHWEST;
  74.         gbc.fill = GridBagConstraints.VERTICAL;
  75.         gbc.insets = new Insets(0,0,0,0);
  76.         gridBagLayout.setConstraints(panel1, gbc);
  77.         add(panel1);
  78.         button1 = new java.awt.Button("Open...");
  79.         button1.reshape(10,10,80,23);
  80.         button1.setBackground(new Color(16777215));
  81.         panel1.add(button1);
  82.         button2 = new java.awt.Button("Save");
  83.         button2.reshape(10,40,80,23);
  84.         button2.setBackground(new Color(16777215));
  85.         panel1.add(button2);
  86.         panel2 = new java.awt.Panel();
  87.         panel2.setLayout(new BorderLayout(0,0));
  88.         panel2.reshape(insets().left + 125,insets().top + 0,433,441);
  89.         panel2.setForeground(new Color(0));
  90.         panel2.setBackground(new Color(16777215));
  91.         gbc = new GridBagConstraints();
  92.         gbc.gridx = 2;
  93.         gbc.gridy = 0;
  94.         gbc.weightx = 1;
  95.         gbc.weighty = 1;
  96.         gbc.anchor = GridBagConstraints.NORTHEAST;
  97.         gbc.fill = GridBagConstraints.BOTH;
  98.         gbc.insets = new Insets(0,0,0,0);
  99.         gridBagLayout.setConstraints(panel2, gbc);
  100.         add(panel2);
  101.         textArea1 = new java.awt.TextArea();
  102.         textArea1.reshape(0,0,433,441);
  103.         textArea1.setFont(new Font("Helvetica", Font.PLAIN, 10));
  104.         panel2.add("Center", textArea1);
  105.         setTitle("Untitled");
  106.         //}}
  107.         
  108.         show();
  109.     }
  110.  
  111. /* Public Methods. */
  112.     
  113.     // Show the window.
  114.     public synchronized void show()
  115.     {
  116.         move(50, 50);
  117.         super.show();
  118.     }
  119.  
  120.     // Specific event handler.
  121.     public boolean handleEvent(Event event)
  122.     {
  123.         if (event.id == Event.WINDOW_DESTROY)
  124.         {
  125.             hide();         // hide the Frame
  126.             dispose();      // tell windowing system to free resources
  127.             return true;
  128.         }
  129.  
  130.         if (event.target == button1 && event.id == Event.ACTION_EVENT)
  131.         {
  132.             button1_Clicked(event);
  133.             return true;
  134.         }
  135.  
  136.         if (event.target == button2 && event.id == Event.ACTION_EVENT)
  137.         {
  138.             button2_Clicked(event);
  139.             return true;
  140.         }
  141.  
  142.         return super.handleEvent(event);
  143.     }
  144.  
  145.     // The big action switch.
  146.     public boolean action(Event event, Object arg)
  147.     {
  148.         if (event.target instanceof java.awt.MenuItem)
  149.         {
  150.             String Label = (String) arg;
  151.  
  152.             if (Label.equalsIgnoreCase("Quit"))
  153.             {
  154.                 System.exit(0);
  155.             }
  156.             else if (Label.equalsIgnoreCase("Open…"))
  157.             {
  158.                 selectedOpen();
  159.                 return true;
  160.             }
  161.             else if (Label.equalsIgnoreCase("New"))
  162.             {
  163.                 selectedNew();
  164.                 return true;
  165.             }
  166.             else if (Label.equalsIgnoreCase("Close"))
  167.             {
  168.                 selectedClose();
  169.                 return true;
  170.             }
  171.             else if (Label.equalsIgnoreCase("Save"))
  172.             {
  173.                 selectedSave();
  174.                 return true;
  175.             }
  176.             else if (Label.equalsIgnoreCase("Save As..."))
  177.             {
  178.                 selectedSaveAs();
  179.                 return true;
  180.             }
  181.         }
  182.  
  183.         return super.action(event, arg);
  184.     }
  185.  
  186.     // Work routine to open files.
  187.     public static void openFile (Edit app, File theFile)
  188.     {
  189.         FileInputStream fileStream = null;
  190.         DataInputStream dataStream = null;
  191.  
  192.         try
  193.         {
  194.             fileStream = new FileInputStream(theFile);
  195.             dataStream = new DataInputStream(fileStream);
  196.  
  197.             app.fFile = theFile;
  198.             app.restore(dataStream);
  199.             app.setTitle(theFile.getName());
  200.  
  201.         }
  202.         catch (Throwable e)
  203.         {
  204.             if (app != null)
  205.             {
  206.                 app.selectedClose();
  207.             }
  208.  
  209.             System.err.println(e.toString());
  210.         }
  211.         finally
  212.         {
  213.             try
  214.             {
  215.                 if (dataStream != null) dataStream.close();
  216.             }
  217.             catch (Throwable e)
  218.             {
  219.             }
  220.  
  221.             try
  222.             {
  223.                 if (fileStream != null) fileStream.close();
  224.             }
  225.             catch (Throwable e)
  226.             {
  227.             }
  228.         }
  229.     }
  230.  
  231.     // Called to choose a file to open.
  232.     public synchronized void selectedOpen()
  233.     {
  234.         Edit app;
  235.         FileDialog fileDialog = new FileDialog(this, "Open…");
  236.         fileDialog.show();
  237.  
  238.         fFile =
  239.             new File
  240.             (
  241.                 fileDialog.getDirectory(),
  242.                 fileDialog.getFile()
  243.             );
  244.  
  245.         app = new Edit();
  246.         openFile(app, fFile);
  247.     }
  248.  
  249.     // Create a new window.
  250.     public void selectedNew()
  251.     {
  252.         Edit        app = new Edit();
  253.     }
  254.  
  255.     // Close a window.
  256.     public void selectedClose()
  257.     {
  258.         hide();         // hide the Frame
  259.         dispose();      // tell windowing system to free resources
  260.     }
  261.  
  262.     // Save a file.
  263.     public synchronized void selectedSave()
  264.     {
  265.         FileOutputStream fileStream = null;
  266.         PrintStream printStream = null;
  267.  
  268.         if (fFile == null)
  269.         {
  270.             FileDialog fileDialog;
  271.  
  272.             fileDialog =
  273.                 new FileDialog
  274.                 (
  275.                     this,
  276.                     "Save",
  277.                     java.awt.FileDialog.SAVE
  278.                 );
  279.  
  280.             fileDialog.show();
  281.  
  282.             fFile =
  283.                 new File
  284.                 (
  285.                     fileDialog.getDirectory(),
  286.                     fileDialog.getFile()
  287.                 );
  288.         }
  289.  
  290.         try
  291.         {
  292.             fileStream = new FileOutputStream(fFile);
  293.             printStream = new PrintStream(fileStream);
  294.             store(printStream);
  295.         }
  296.         catch (Throwable e)
  297.         {
  298.             System.err.println(e.toString());
  299.         }
  300.         finally
  301.         {
  302.             try
  303.             {
  304.                 if (printStream != null) printStream.close();
  305.             }
  306.             catch (Throwable e)
  307.             {
  308.             }
  309.  
  310.             try
  311.             {
  312.                 if (fileStream != null) fileStream.close();
  313.             }
  314.             catch (Throwable e)
  315.             {
  316.             }
  317.         }
  318.     }
  319.  
  320.     // Save a file as the given file.
  321.     public void selectedSaveAs()
  322.     {
  323.         fFile = null;
  324.         selectedSave();
  325.     }
  326.  
  327.     // Restore from a stream.
  328.     public void restore(DataInputStream stream)
  329.     {
  330.         try
  331.         {
  332.             String        textLine;
  333.     
  334.             textLine = stream.readLine();
  335.     
  336.             while (textLine != null)
  337.             {
  338.                 textArea1.appendText(textLine);
  339.                 textArea1.appendText("\n");
  340.                 textLine = stream.readLine();
  341.             }
  342.         }
  343.         catch (Throwable e)
  344.         {
  345.         }
  346.     }
  347.  
  348.     // Store to a stream.
  349.     public void store(PrintStream stream)
  350.     {
  351.         stream.println(textArea1.getText());
  352.     }
  353.  
  354. /* Public Static Methods. */
  355.  
  356.     // Application entry point.
  357.     public static void main(String args[])
  358.     {
  359.         if (args.length == 0)
  360.         {
  361.             Edit app;
  362.             app = new Edit();
  363.         }
  364.         else
  365.         {
  366.             File theFile;
  367.             Edit app;
  368.  
  369.             for (int i = 0;  i < args.length;  i++)
  370.             {
  371.                 theFile = new File(args[i]);
  372.  
  373.                 if (theFile.canRead())
  374.                 {
  375.                     app = new Edit();
  376.                     openFile(app, theFile);
  377.                 }
  378.             }
  379.         }
  380.     }
  381.  
  382. /* Private Members. */
  383.  
  384.     File            fFile = null;
  385.  
  386.     //{{DECLARE_MENUS
  387.     java.awt.MenuBar menuBar;
  388.     java.awt.Menu fileMenu;
  389.     java.awt.Menu editMenu;
  390.     //}}
  391.  
  392.     //{{DECLARE_CONTROLS
  393.     java.awt.Panel panel1;
  394.     java.awt.Button button1;
  395.     java.awt.Button button2;
  396.     java.awt.Panel panel2;
  397.     java.awt.TextArea textArea1;
  398.     //}}
  399.  
  400.     // Open button event handler.
  401.     void button1_Clicked(Event event)
  402.     {
  403.         selectedOpen();
  404.     }
  405.  
  406.     // Save button event handler.
  407.     void button2_Clicked(Event event)
  408.     {
  409.         selectedSave();
  410.     }
  411. }
  412.